﻿/*=============================================================================== 
 * author:csyingwu
 * date:2012/9/28
 * 
 * 
 */
//============================================================================
using System;
using System.Linq;
using System.Text;
using Kaidou.Model;
using System.Data;
using System.Data.SqlClient;
using Kaidou.Util;
using System.Collections;



namespace Kaidou.Persister
{
    public class Common
    {

        public int getSequenceNextVal(String year, String seqtable)
        {
            int seqno=0;
            try
            {

                string sql = "SEQNO_SEQ_PRO";
                SqlDB db = new SqlDB();
                SqlParameter[] sqlParameter = { new SqlParameter("@return", SqlDbType.Int), new SqlParameter("@year", SqlDbType.Int), new SqlParameter("@seqtable", SqlDbType.VarChar) };
                sqlParameter[0].Direction = ParameterDirection.ReturnValue;
                sqlParameter[1].Direction = ParameterDirection.Input;
                sqlParameter[1].Value = int.Parse(year);
                sqlParameter[2].Direction = ParameterDirection.Input;
                sqlParameter[2].Value = seqtable;
                db.ExecuteProcedure(sql, ref sqlParameter);
                seqno = int.Parse(sqlParameter[0].Value.ToString());
                db.Close();
            }
            catch
            {
                LogManager.error("Common - getSequenceNextVal");
            }
            return seqno;
        }

        /**zhou-anyuan
        * current Timestamp を取得
        * @param con コネクション
        * @return java.sql.Timestamp
        * @throws PersisterSQLException
        */
        public DateTime getCurrentTimestamp(SqlDB con)
        {
            DateTime ts = DateTime.Now;
            //PreparedStatement ps = null;
            //ResultSet rs = null;

            try
            {
                //ps = con.prepareStatement("VALUES CURRENT TIMESTAMP");
                SqlDataReader rdr = con.GetDataCmd("SELECT GETDATE() as cnt", null);
                DataTable rs = DBUnit.ConvertDataReaderToDataTable(rdr);

                ts = DBUnit.GetDateTime(rs.Rows[0]["cnt"]);

            }
            catch (Exception e)
            {
                LogManager.fatal("Common - getCurrentTimestamp " + ts.ToString() + " !!");
                throw e;
            }
            //finally
            //{

            //    try
            //    {
            //        con.Close();
            //    }
            //    catch (Exception e)
            //    {
            //        LogManager.fatal("Common - getCurrentTimestamp - DB.close" + e.ToString());
            //    }
            //}

            return ts;
        }

        /**zhou-anyuan
        * current date を取得
        * @param con コネクション
        * @return java.sql.Date
        * @throws PersisterSQLException
        */
        public DateTime getCurrentDate(SqlDB con)
        {
            DateTime date = DateTime.Now;
            //PreparedStatement ps = null;
            //ResultSet rs = null;
            try
            {
                SqlDataReader rdr = con.GetDataCmd("SELECT GETDATE()", null);
                DataTable rs = DBUnit.ConvertDataReaderToDataTable(rdr);

                date = DBUnit.GetDateTime(rs.Rows[0][0]);
            }
            catch (Exception e)
            {
                LogManager.fatal("Common - getCurrentDate " + date.ToString() + " !!");

                throw e;
            }
            //finally
            //{
            //    try
            //    {
            //        con.Close();
            //    }
            //    catch (Exception e)
            //    {
            //        LogManager.fatal("Common - getCurrentTimestamp - DB.close" + e.ToString());
            //    }
            //}

            return date;
        }
       
    }
}
